<?php


use Biz\model\GoodsTestActivity\FineGoodsContentModel;
use Biz\service\wiki\Wiki as sWiki;
use Doraemon\pockets\constInterface\FormatScene;
use Doraemon\pockets\IdSender\IdSender;
use Doraemon\service\Format\Blank\HandleBlank;
use Doraemon\service\Format\Note\HandleNote;
use Doraemon\tools\IdParse;

/**
 * 获取wiki的好物轻测-评测内容
 * Class GetTestReportListAction
 */
class GetTestReportListAction extends ActionPlugin
{
    const USER_LEVEL = self::LEVEL_NOTHING;

    const DEFAULT_LIMIT = 20;

    private $basicId = 0;

    public function main()
    {
        $wiki_id = $this->getRequest()->getQuery('wiki_id');
        $spuInfo = sWiki::getWikiBaseInfos([$wiki_id]);
        $spuInfo = ($spuInfo && isset($spuInfo[$wiki_id])) ? $spuInfo[$wiki_id] : [];
        $basicId = $spuInfo ? $spuInfo['basic_id'] : 0;
        $this->basicId = $basicId;
        $data = [];
        if ($this->basicId) {
            $objIds = FineGoodsContentModel::all(['basic_id' => $this->basicId, 'is_delete[!]' => FineGoodsContentModel::IS_DELETE_TRUE, 'ORDER' => ['points' => 'DESC', 'id' => 'DESC']], 'obj_id');
            $blank_ids = [];
            $note_ids = [];
            foreach ($objIds as $obj_id) {
                switch (IdParse::getType($obj_id)) {
                    case IdSender::TYPE_PHOTO:
                    case IdSender::TYPE_ANSWER:
                        array_push($note_ids, $obj_id);
                        break;
                    case IdSender::TYPE_BLANK:
                        array_push($blank_ids, $obj_id);
                        break;
                }
            }

            $blank_list_map = $this->getBlankListMap($blank_ids);
            $note_list_map = $this->getNoteListMap($note_ids);

            foreach ($objIds as $obj_id) {
                switch (IdParse::getType($obj_id)) {
                    case IdSender::TYPE_PHOTO:
                    case IdSender::TYPE_ANSWER:
                        if (isset($note_list_map[$obj_id])) {
                            $data[] = $note_list_map[$obj_id];
                        }
                        break;
                    case IdSender::TYPE_BLANK:
                        if (isset($blank_list_map[$obj_id])) {
                            $data[] = $blank_list_map[$obj_id];
                        }
                        break;
                }
            }
        }
        $this->setSuccess(['list' => $data, 'is_over' => 1]);
    }

    private function getBlankListMap($blank_ids)
    {
        $blank_list_map = [];
        if ($blank_ids && is_array($blank_ids)) {
            $blank_list_map = HandleBlank::handleBlank($blank_ids, FormatScene::SEARCH_FEED, $this->uid);
            foreach ($blank_list_map as $blankId => $blank) {
                $uid = $blank['blank']['user_info']['uid'];
                if (in_array($uid, \Biz\service\wiki\WikiContent::FILTER_UIDS)) {
                    unset($blank_list_map[$blankId]);
                    continue;
                }
                $blank_list_map[$blankId]['statSign'] = '';
            }
        }
        return $blank_list_map;
    }

    private function getNoteListMap($note_ids)
    {
        $note_list_map = [];
        if ($note_ids && is_array($note_ids)) {
            $note_list_map = HandleNote::handleNote($note_ids, FormatScene::FEED, $this->uid);
            //$note_list = sPhotos::getPhotoInfos($note_ids, $this->uid);
            //$uids = array_map([new IdParse(), 'getUid'], $note_ids);
            if ($note_list_map && is_array($note_list_map)) {
                foreach ($note_list_map as $note_id => $note) {
                    $uid = $note['photo']['user_info']['uid'];
                    if (in_array($uid, \Biz\service\wiki\WikiContent::FILTER_UIDS)) {
                        unset($note_list_map[$note_id]);
                        continue;
                    }
                    $note_list_map[$note_id]['statSign'] = '';
                }
            }
        }
        return $note_list_map;
    }
}
